package Presentacion.contabilidad;

import clases.contabilidad.Concepto;
import javax.swing.table.DefaultTableModel;

public final class frmConsultaConcepto extends javax.swing.JInternalFrame {
    
    private static int pagina;
    private static String sql;
    public static Concepto objConcepto;
    public static dlgConcepto concepto;

    /**
     * Creates new form frmConsultaConcepto
     */
    public frmConsultaConcepto() {
        initComponents();
        setResizable(false);
        objConcepto = new Concepto();
        pagina = 1;
        sql = objConcepto.SqlGeneral();
        generartabla();
        inicializar();       
    }
    
    public void llenarDialogo(int fila) {
        objConcepto.llenardatosConcepto(tblconcepto.getValueAt(fila, 0).toString());
        concepto = new dlgConcepto(null, true);
        concepto.visiblidad(false);
        concepto.llenardatos();
        concepto.setVisible(true);
    }
    
    public void mostrarConcepto() {
        concepto = new dlgConcepto(null, true);
        concepto.visiblidad(true);
        concepto.setVisible(true);
    }
    
    public static void generartabla() {
        objConcepto.ConsultaConcepto(new DefaultTableModel(), tblconcepto,
                sql, lbltotalpaginas, lblpagina, pagina);
    }
    
    public void EliminarConcepto(int fila) {
        if (objConcepto.EliminarConcepto(
                tblconcepto.getValueAt(fila, 0).toString()) == true) {
            generartabla();
        }
        
    }
    
    private void inicializar() {
        txtconcepto.setText("");
        txtprecio.setVisible(false);
        txtprecio.setText("");
        txtconcepto.setVisible(false);
        cbotipo.setSelectedIndex(0);
        cbotipo.setVisible(false);
        txtcuenta.setText("");
        txtcuenta.setVisible(false);
        cbogrupo.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblconcepto = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnprimero = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel4 = new javax.swing.JLabel();
        lblpagina = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbltotalpaginas = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnsiguiente = new javax.swing.JButton();
        btnultimo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbocriterio = new javax.swing.JComboBox();
        txtprecio = new javax.swing.JTextField();
        txtconcepto = new javax.swing.JTextField();
        cbotipo = new javax.swing.JComboBox();
        txtcuenta = new javax.swing.JTextField();
        cbogrupo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblconcepto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblconcepto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblconceptoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblconcepto);

        jToolBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnprimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/primer.png"))); // NOI18N
        btnprimero.setFocusable(false);
        btnprimero.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnprimero.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnprimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnprimeroActionPerformed(evt);
            }
        });
        jToolBar1.add(btnprimero);

        btnatras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/atras.png"))); // NOI18N
        btnatras.setToolTipText("");
        btnatras.setFocusable(false);
        btnatras.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnatras.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        jToolBar1.add(btnatras);
        jToolBar1.add(jSeparator1);

        jLabel4.setText("Pagina ");
        jToolBar1.add(jLabel4);

        lblpagina.setText("jLabel2");
        jToolBar1.add(lblpagina);

        jLabel5.setText(" de ");
        jToolBar1.add(jLabel5);

        lbltotalpaginas.setText("jLabel1");
        jToolBar1.add(lbltotalpaginas);
        jToolBar1.add(jSeparator2);

        btnsiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/siguiente.png"))); // NOI18N
        btnsiguiente.setFocusable(false);
        btnsiguiente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsiguiente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsiguienteActionPerformed(evt);
            }
        });
        jToolBar1.add(btnsiguiente);

        btnultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ultimos.png"))); // NOI18N
        btnultimo.setFocusable(false);
        btnultimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnultimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnultimoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnultimo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BUSQUEDA POR:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("CRITERIO");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 31, -1, -1));

        cbocriterio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODO", "CONCEPTO", "GRUPO", "CUENTA", "PRECIO", "TIPO" }));
        cbocriterio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocriterioActionPerformed(evt);
            }
        });
        jPanel1.add(cbocriterio, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 51, 90, -1));

        txtprecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtprecioKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtprecioKeyTyped(evt);
            }
        });
        jPanel1.add(txtprecio, new org.netbeans.lib.awtextra.AbsoluteConstraints(148, 51, 91, -1));

        txtconcepto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtconceptoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtconceptoKeyTyped(evt);
            }
        });
        jPanel1.add(txtconcepto, new org.netbeans.lib.awtextra.AbsoluteConstraints(148, 51, 260, -1));

        cbotipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "COBRO", "PAGO" }));
        cbotipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbotipoActionPerformed(evt);
            }
        });
        jPanel1.add(cbotipo, new org.netbeans.lib.awtextra.AbsoluteConstraints(148, 51, -1, -1));

        txtcuenta.setText("jTextField1");
        txtcuenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtcuentaKeyReleased(evt);
            }
        });
        jPanel1.add(txtcuenta, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 260, -1));

        cbogrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbogrupoActionPerformed(evt);
            }
        });
        jPanel1.add(cbogrupo, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 220, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnsiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsiguienteActionPerformed
        if (objConcepto.objPaginacion.getTotalpaginas() > pagina) {
            pagina++;
            generartabla();
        }
    }//GEN-LAST:event_btnsiguienteActionPerformed

    private void btnultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnultimoActionPerformed
        pagina = objConcepto.objPaginacion.getTotalpaginas();
        generartabla();
    }//GEN-LAST:event_btnultimoActionPerformed

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
        if (pagina > 1) {
            pagina--;
            generartabla();
        }
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnprimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnprimeroActionPerformed
        pagina = 1;
        generartabla();
    }//GEN-LAST:event_btnprimeroActionPerformed

    private void cbocriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocriterioActionPerformed
        inicializar();
        if (cbocriterio.getSelectedItem() == "TODO") {
            pagina = 1;
            sql = objConcepto.SqlGeneral();
            generartabla();
            
        } else if (cbocriterio.getSelectedItem() == "CONCEPTO") {
            txtconcepto.setVisible(true);
            txtconcepto.requestFocus();
            
        } else if (cbocriterio.getSelectedItem() == "PRECIO") {
            txtprecio.setVisible(true);
            txtprecio.requestFocus();
            
        } else if (cbocriterio.getSelectedItem() == "TIPO") {
            cbotipo.setVisible(true);
            cbotipo.requestFocus();
            
        } else if (cbocriterio.getSelectedItem() == "GRUPO") {
            cbogrupo.setVisible(true);
            objConcepto.listarGrupo(cbogrupo);
            cbogrupo.requestFocus();
            
        } else if (cbocriterio.getSelectedItem() == "CUENTA") {
            txtcuenta.setVisible(true);
            txtcuenta.requestFocus();
            
        }
    }//GEN-LAST:event_cbocriterioActionPerformed

    private void txtprecioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtprecioKeyReleased
        if ((global.Validaciones.comprobarNulo(txtprecio))) {
            pagina = 1;
            sql = objConcepto.SqlPrecio(Integer.parseInt(txtprecio.getText()));
            generartabla();
        } else {
            sql = objConcepto.SqlGeneral();
            generartabla();
        }

    }//GEN-LAST:event_txtprecioKeyReleased

    private void txtprecioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtprecioKeyTyped
        global.Validaciones.soloNumeros(evt, txtprecio, 15);
    }//GEN-LAST:event_txtprecioKeyTyped

    private void txtconceptoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtconceptoKeyReleased
        global.Validaciones.convertirMayuscula(txtconcepto);
        if ((global.Validaciones.comprobarNulo(txtconcepto))) {
            pagina = 1;
            sql = objConcepto.SqlConcepto(txtconcepto.getText());
            generartabla();
        } else {
            sql = objConcepto.SqlGeneral();
            generartabla();
        }

    }//GEN-LAST:event_txtconceptoKeyReleased

    private void txtconceptoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtconceptoKeyTyped
        global.Validaciones.soloLetras(evt, txtconcepto);
    }//GEN-LAST:event_txtconceptoKeyTyped

    private void cbotipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbotipoActionPerformed
        if (cbotipo.getSelectedIndex() != 0) {
            sql = objConcepto.Sqltipo(cbotipo.getSelectedItem().toString());
            generartabla();
        } else {
            sql = objConcepto.SqlGeneral();
            generartabla();
        }
    }//GEN-LAST:event_cbotipoActionPerformed

    private void tblconceptoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblconceptoMouseClicked
        int click = evt.getClickCount();
        if (click == 2) {
            llenarDialogo(tblconcepto.getSelectedRow());
            
        }
    }//GEN-LAST:event_tblconceptoMouseClicked

    private void txtcuentaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcuentaKeyReleased
        global.Validaciones.convertirMayuscula(txtcuenta);
        if ((global.Validaciones.comprobarNulo(txtcuenta))) {
            pagina = 1;
            sql = objConcepto.SqlCuenta(txtcuenta.getText());
            generartabla();
        } else {
            sql = objConcepto.SqlGeneral();
            generartabla();
        }

    }//GEN-LAST:event_txtcuentaKeyReleased

    private void cbogrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbogrupoActionPerformed
        if (cbogrupo.getSelectedIndex() != 0) {
            sql = objConcepto.SqlGrupo(cbogrupo.getSelectedItem().toString());
            generartabla();
        } else {
            sql = objConcepto.SqlGeneral();
            generartabla();
        }
    }//GEN-LAST:event_cbogrupoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmConsultaConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmConsultaConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmConsultaConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmConsultaConcepto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frmConsultaConcepto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnatras;
    private javax.swing.JButton btnprimero;
    private javax.swing.JButton btnsiguiente;
    private javax.swing.JButton btnultimo;
    private javax.swing.JComboBox cbocriterio;
    private javax.swing.JComboBox cbogrupo;
    private javax.swing.JComboBox cbotipo;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private static javax.swing.JLabel lblpagina;
    private static javax.swing.JLabel lbltotalpaginas;
    public static javax.swing.JTable tblconcepto;
    private javax.swing.JTextField txtconcepto;
    private javax.swing.JTextField txtcuenta;
    private javax.swing.JTextField txtprecio;
    // End of variables declaration//GEN-END:variables
}
